package northpool.operator.el;



import northpool.operator.el.parameter.Parameter;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author BaiChenguang
 * @class Context
 */
public class ElContext implements EvaluationContext {

    private Map<String, Object> map = new HashMap<>();

    public EvaluationContext add(String name, Object value) {
        if (value instanceof BigDecimal) {
            this.add(name, (BigDecimal) value);
        } else if(value instanceof Integer) {
            this.add(name, (Integer) value);
        } else if(value instanceof Long) {
            this.add(name, (Long) value);
        } else if(value instanceof Float) {
            this.add(name, (Float) value);
        } else if(value instanceof Double) {
            this.add(name, (Double) value);
        } else if(value instanceof Boolean) {
            this.add(name, (Boolean) value);
        } else if(value instanceof String) {
            this.add(name, (String) value);
        } else {
            throw new RuntimeException("ElContext.add()不支持的类型:" + value.getClass().getName());
        }
        return this;
    }

    public EvaluationContext add(String name, BigDecimal value) {
        this.map.put(name, value);
        return this;
    }

    public EvaluationContext add(String name, BigDecimal[] value) {
        this.map.put(name, value);
        return this;
    }

    public EvaluationContext add(String name, String value) {
        this.map.put(name, value);
        return this;
    }

    public EvaluationContext add(String name, String[] value) {
        this.map.put(name, value);
        return this;
    }

    public EvaluationContext add(String name, Boolean value) {
        this.map.put(name, value);
        return this;
    }

    public EvaluationContext add(String name, Boolean[] value) {
        this.map.put(name, value);
        return this;
    }

    public EvaluationContext add(String name, Integer value) {
        this.map.put(name, BigDecimal.valueOf(value));
        return this;
    }

    public EvaluationContext add(String name, Integer[] value) {
        this.map.put(name, value);
        return this;
    }

    public EvaluationContext add(String name, Long value) {
        this.map.put(name, BigDecimal.valueOf(value));
        return this;
    }

    public EvaluationContext add(String name, Long[] value) {
        this.map.put(name, value);
        return this;
    }

    public EvaluationContext add(String name, Float value) {
        this.map.put(name, new BigDecimal(value.toString()));
        return this;
    }

    public EvaluationContext add(String name, Float[] value) {
        this.map.put(name, value);
        return this;
    }

    public EvaluationContext add(String name, Double value) {
        this.map.put(name, new BigDecimal(value.toString()));
        return this;
    }

    public EvaluationContext add(String name, Double[] value) {
        this.map.put(name, value);
        return this;
    }

    public EvaluationContext add(String name, Parameter value) {
        this.map.put(name, value.getValue());
        return this;
    }

    public EvaluationContext add(String scope, String name, Parameter value) {
        this.map.put(scope + "." + name, value.getValue());
        return this;
    }

    @Override
    public Object getVariable(String variableName) {
        Object value = this.map.get(variableName);
        if(value == null) {
            return  "null";
        } else {
            return value;
        }
    }
}
